import { reactive, ref } from "vue";

export const validatePass = (rule, value, callback) => {
  console.log(value, "kkkkk");
  if (value === "") {
    callback(new Error("用户名不能为空"));
    return;
  }
  callback();
};

export const validatephone = (rule, value, callback) => {
  let reg =
    /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
  console.log(value, "ytytyty");
  if (value ==="") {
    callback(new Error("手机号码不能为空"));
  } else if (!reg.test(value)) {
    callback(new Error("手机号码输入错误"));
  } else {
    callback();
  }
};

export const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("密码不能为空"));
    return;
  }
  callback();
};
export const checkAge = (rule, value, callback) => {
  console.log(value);
  if (value.length < 2) {
    callback(new Error("验证码少5于位"));
    return;
  }
  callback();
};

export const rules = reactive({
  username: [{ validator: validatePass, trigger: "blur" }],
  password: [{ validator: validatePass2, trigger: "blur" }],
  Verification: [{ validator: checkAge, trigger: "blur" }],
});

export const useSubmitForm = (successForm, failForm) => (formEl) => {
  console.log(formEl,"iiii");
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      if (successForm) {
        successForm();
      }
      console.log("submit!");
    } else if (failForm) {
      failForm();
      console.log("error submit!");
      return false;
    }
  });
};
